<?php
declare(strict_types=1);

namespace app\admin\business;

use app\common\model\CommonBase;
use app\common\model\FeedbackField as FeedbackFieldModel;

class FeedbackField extends AdminBase
{
    private $_feedbackFieldObj = null;

    public function __construct(string $websiteCode)
    {
        $this->_websiteCode      = $websiteCode;
        $this->_feedbackFieldObj = new FeedbackFieldModel([], $websiteCode);
    }

    public function getAllFeedbackField(array $where = []): array
    {
        $rows = [];
        try {
            $rows = $this->_feedbackFieldObj->getAllFeedbackField($where);
        } catch (\Exception $e) {
            logErr('读取自定义表单的所有字段失败(后台)', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return $rows;
    }

    public function getFeedbackFieldList(int $page = 1, int $limit = 10, array $where = []): array
    {
        $res   = [];
        $count = 0;
        try {
            $res   = $this->_feedbackFieldObj->getFeedbackFieldList($page, $limit, $where);
            $count = $this->_feedbackFieldObj->getFeedbackFieldCount($where);
        } catch (\Exception $e) {
            logErr('读取自定义表单的字段列表失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        $rows = $res->toArray();

        foreach ($rows as $key => $row) {
            $rows[$key]['status_name']     = $row['status'] == self::_IS_ENABLE_STATUS ? '启用' : '禁用';
            $rows[$key]['field_type_name'] = CommonBase::FIELD_TYPE_NAME_MAP[$row['field_type']] ?? '';
            $rows[$key]['is_require_name'] = $row['is_require'] == CommonBase::FIELD_IS_REQUIRE ? '必填' : '选填';
            $rows[$key]['is_system_name']  = $row['is_system'] == CommonBase::FIELD_IS_SYSTEM ? '系统' : '自定义';
        }

        return ['rows' => $rows, 'count' => $count];
    }

    public function addFeedbackField(array $data): array
    {
        try {
            return [self::_RESULT => $this->_feedbackFieldObj->addFeedbackField($data), self::_MESSAGE => self::_ADD_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('添加自定义表单的字段失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function findFeedbackFieldById(int $id): array
    {
        $row = null;
        try {
            $row = $this->_feedbackFieldObj->findFeedbackFieldById($id);
        } catch (\Exception $e) {
            logErr('通过主键读取自定义表单的字段信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        if (empty($row)) {
            return [];
        }

        $feedbackField = $row->toArray();

        $feedbackField['status_name']     = $row['status'] == self::_IS_ENABLE_STATUS ? '启用' : '禁用';
        $feedbackField['field_type_name'] = CommonBase::FIELD_TYPE_NAME_MAP[$row['field_type']] ?? '';
        $feedbackField['is_require_name'] = $row['is_require'] == CommonBase::FIELD_IS_REQUIRE ? '必填' : '选填';

        return $feedbackField;
    }

    public function updateFeedbackField(int $id, array $data): array
    {
        try {
            return [self::_RESULT => $this->_feedbackFieldObj->updateFeedbackField($id, $data), self::_MESSAGE => self::_UPDATE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键更新自定义表单的字段信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function batchFeedbackFieldSort(array $data): array
    {
        try {
            return [self::_RESULT => $this->_feedbackFieldObj->batchFeedbackFieldSort($data), self::_MESSAGE => self::_SORT_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键更新自定义表单的字段排序失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function deleteFeedbackField(int $id): array
    {
        try {
            return [self::_RESULT => $this->_feedbackFieldObj->deleteFeedbackField($id), self::_MESSAGE => self::_DELETE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键删除自定义表单的字段失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function clearFeedbackFieldCache(): bool
    {
        return $this->_feedbackFieldObj->clearFeedbackFieldCache();
    }
}
